package com.whfc.ms.api.fse.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fse.dto.FseCraneDataDTO;
import com.whfc.fse.dto.FseCraneDataLogDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fse.service.MsFseCraneDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/4/9
 */
@RestController
@RequestMapping("/ms/api/crane/data")
public class FseCraneDataController extends BaseController {
    @Autowired
    private MsFseCraneDataService msFseCraneDataService;

    @GetMapping("/latest")
    public Result latest(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("craneId") Integer craneId) {
        logger.info("获取塔机及时数据,loginer:{},craneId:{}", user.getUsername(), craneId);
        FseCraneDataDTO fseCraneDataDTO = msFseCraneDataService.latest(craneId);
        return ResultUtil.success(fseCraneDataDTO);
    }

    @GetMapping("/anon/latest")
    public Result anonLatest(@RequestParam("craneId") Integer craneId) {
        logger.info("匿名获取塔机及时数据, craneId:{}", craneId);
        FseCraneDataDTO fseCraneDataDTO = msFseCraneDataService.latest(craneId);
        return ResultUtil.success(fseCraneDataDTO);
    }

    @GetMapping(value = "/log", params = "date")
    public Result log(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestParam("craneId") Integer craneId,
                      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("获取塔机监控的历史数据,loginer:{},craneId:{},date:{}", user.getUsername(), craneId, date);
        ListData<FseCraneDataLogDTO> listData = msFseCraneDataService.log(craneId, date);
        return ResultUtil.success(listData);
    }

    @GetMapping(value = "/log", params = {"startTime", "endTime"})
    public Result log(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestParam("craneId") Integer craneId,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("startTime") Date startTime,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("endTime") Date endTime) {
        logger.info("获取塔机监控的历史数据,loginer:{},craneId:{},startTime:{},endTime:{}", user.getUsername(), craneId, startTime, endTime);
        ListData<FseCraneDataLogDTO> listData = msFseCraneDataService.log(craneId, startTime, endTime);
        return ResultUtil.success(listData);
    }

}
